package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.OrderperFormMap;
import com.iemes.entity.PBeforeCheckFormMap;
import com.iemes.entity.PrepareFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.MouldCheckStandardMapper;
import com.iemes.mapper.mould.PrepareOrderMapper;
import com.iemes.mapper.shape.ShapeMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.MyDateUtils;

@RequestMapping("/prepareOrder/")
@Controller
public class PrepareOrderController extends BaseController {

	@Inject
	private PrepareOrderMapper prepareOrderMapper;
	
	@Inject
	private ShapeMapper shapeMapper;
	
	@Inject
	private MouldCheckStandardMapper mouldCheckStandardMapper;
	
	// 查看预排指令表，显示按钮。
	@RequestMapping("prepareOrder_all")
	public String enterstorage_form(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/prepare/prepareOrder_list";
	}
	
	// 预排指令表分页
	@ResponseBody
	@RequestMapping("prepareOrder_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		List<Map<String, Object>> order = prepareOrderMapper.getAll(orderFormMap);
		LuogicPaging(pageNow, pageSize, order);
		return pageView;
	}
	
	@RequestMapping("prepareOrder_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id获得预排指令表
			OrderFormMap order=prepareOrderMapper.getById(id);
			model.addAttribute("order",order);
			//获得相同orderId的备模指令表
			PrepareFormMap prepare=baseMapper.findbyFrist("orderId", order.get("id").toString(), PrepareFormMap.class);
			model.addAttribute("prepare",prepare);
		}
		return Common.BACKGROUND_PATH + "/mould/prepare/prepareOrder_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="prepareOrder_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="预排指令",methods="预排指令表-修改预排指令表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		// 新增标签子项
		String orderId=request.getParameter("orderFormMap.id");
		OrderFormMap order=prepareOrderMapper.getById(orderId);
		//设置《预排指令》表的“上模时间”提前3小时为“计划备模时间”
		String date=order.get("up_moldTime").toString();
		//3小时等于10800000毫秒
		long millisecond=10800000;
		PrepareFormMap prepareFormMap = getFormMap(PrepareFormMap.class);
		prepareFormMap.put("scheduleTime", MyDateUtils.pTime(date, millisecond));
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1=new Date();
		prepareFormMap.put("date", sdf.format(date1));
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		prepareFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		prepareFormMap.put("orderId", orderId);
		prepareFormMap.put("mouldstatus", 4);
		baseMapper.addEntity(prepareFormMap);
		//把预排表改为已备模状态
		OrderperFormMap orderperFormMap=shapeMapper.getByOrderId(orderId);
		orderperFormMap.put("state", 5);
		baseMapper.editEntity(orderperFormMap);
		//添加备模前模具质量检查表
		PBeforeCheckFormMap pBeforeCheckFormMap=getFormMap(PBeforeCheckFormMap.class);
		pBeforeCheckFormMap.put("prepareId", prepareFormMap.get("id"));
		pBeforeCheckFormMap.put("checkPeopleId", 11);
		baseMapper.addEntity(pBeforeCheckFormMap);
		//循环添加备模前模具质量检查表检查标准
		MouldCheckStandardFormMap mouldCheckStandardFormMap = getFormMap(MouldCheckStandardFormMap.class);
		mouldCheckStandardFormMap.put("status", 1);
		List<Map<String, Object>> getAllItemBy1=mouldCheckStandardMapper.getAllItemBy1(mouldCheckStandardFormMap);
		for(Map<String, Object> map:getAllItemBy1){
			MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
			mouldUnpassHandleFormMap.put("standardId",map.get("id"));
			mouldUnpassHandleFormMap.put("checkId", pBeforeCheckFormMap.get("id"));
			mouldUnpassHandleFormMap.put("repairPeopleId",11);
			mouldUnpassHandleFormMap.put("supervisorId",11);
			baseMapper.addEntity(mouldUnpassHandleFormMap);
		}
		return "success";
	}
}









